var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function (req, res, next) {
    var db = new sqlite3.Database(path.join(__dirname, "../db/Delphi.s3db"));
    var index = req.query.index;
    req.session.index = index;
    if (index == '0') //专家自我评价
        res.send('done');
    else if (index == '1') { //第一轮调查
        var sqlStr1 = "select * from s_expert where s_expert_id = ?";
        var sqlStr2 = "select * from p_expert where p_expert_id = ?";
        try {
            db.all(sqlStr1, [req.session.username], function (err, rows) {
                if (rows.length == 0) {
                    db.all(sqlStr2, [req.session.username], function (err, rows) {
                        if (rows[0].w_place != null && rows[0].w_time != null && rows[0].edu != null && rows[0].field != null && rows[0].age != null && rows[0].p_title != null) {
                            res.send('done'); //完成专家(职业人士)自我评价
                        } else {
                            res.send('0'); //未完成专家(职业人士)自我评价
                        }
                    });
                } else {
                    if (rows[0].s_title != null && rows[0].v_time != null && rows[0].v_time != null)
                        res.send('done'); //完成专家(学生)自我评价
                    else
                        res.send('0'); //未完成专家(学生)自我评价
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    } else if (index == '2') { //第二轮调查
        var sqlStr1 = "select * from s_expert where s_expert_id = ?";
        var sqlStr2 = "select * from p_expert where p_expert_id = ?";
        var sqlStr3 = "select * from answer1 where expert_id = ?";
        try {
            db.all(sqlStr3, [req.session.username], function (err, rows) {
                if (rows.length == 0) {
                    db.all(sqlStr1, [req.session.username], function (err, rows) {
                        if (rows.length == 0) {
                            db.all(sqlStr2, [req.session.username], function (err, rows) {
                                if (rows[0].w_place != null && rows[0].w_time != null && rows[0].edu != null && rows[0].field != null && rows[0].age != null && rows[0].p_title != null) {
                                    res.send('1'); //完成专家(职业人士)自我评价，但未完成第一轮调查
                                } else {
                                    res.send('0'); //未完成专家(职业人士)自我评价
                                }
                            });
                        } else {
                            if (rows[0].s_title != null && rows[0].v_time != null && rows[0].v_time != null)
                                res.send('1'); //完成专家(学生)自我评价，但未完成第一轮调查
                            else
                                res.send('0'); //未完成专家(学生)自我评价
                        }
                    });
                } else {
                    res.send('done'); //完成第一轮调查
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    } else if (index == '3') { //第三轮调查
        var sqlStr1 = "select * from s_expert where s_expert_id = ?";
        var sqlStr2 = "select * from p_expert where p_expert_id = ?";
        var sqlStr3 = "select * from answer1 where expert_id = ?";
        var sqlStr4 = "select * from answer2_1 where expert_id = ?";
        try {
            db.all(sqlStr4, [req.session.username], function (err, rows) {
                if (rows.length == 0) {
                    db.all(sqlStr3, [req.session.username], function (err, rows) {
                        if (rows.length == 0) {
                            db.all(sqlStr1, [req.session.username], function (err, rows) {
                                if (rows.length == 0) {
                                    db.all(sqlStr2, [req.session.username], function (err, rows) {
                                        if (rows[0].w_place != null && rows[0].w_time != null && rows[0].edu != null && rows[0].field != null && rows[0].age != null && rows[0].p_title != null) {
                                            res.send('1'); //完成专家(职业人士)自我评价，但未完成第一轮调查
                                        } else {
                                            res.send('0'); //未完成专家(职业人士)自我评价
                                        }
                                    });
                                } else {
                                    if (rows[0].s_title != null && rows[0].v_time != null && rows[0].v_time != null)
                                        res.send('1'); //完成专家(学生)自我评价，但未完成第一轮调查
                                    else
                                        res.send('0'); //未完成专家(学生)自我评价
                                }
                            });
                        } else {
                            res.send('2'); //完成第一轮调查，但未完成第二轮调查
                        }
                    });
                } else {
                    res.send('done'); //完成第二轮调查
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    } else if (index == '4') { //第四轮调查
        var sqlStr1 = "select * from s_expert where s_expert_id = ?";
        var sqlStr2 = "select * from p_expert where p_expert_id = ?";
        var sqlStr3 = "select * from answer1 where expert_id = ?";
        var sqlStr4 = "select * from answer2_1 where expert_id = ?";
        var sqlStr5 = "select * from answer3 where expert_id = ?";
        try {
            db.all(sqlStr5, [req.session.username], function (err, rows) {
                if (rows.length == 0) {
                    db.all(sqlStr4, [req.session.username], function (err, rows) {
                        if (rows.length == 0) {
                            db.all(sqlStr3, [req.session.username], function (err, rows) {
                                if (rows.length == 0) {
                                    db.all(sqlStr1, [req.session.username], function (err, rows) {
                                        if (rows.length == 0) {
                                            db.all(sqlStr2, [req.session.username], function (err, rows) {
                                                if (rows[0].w_place != null && rows[0].w_time != null && rows[0].edu != null && rows[0].field != null && rows[0].age != null && rows[0].p_title != null) {
                                                    res.send('1'); //完成专家(职业人士)自我评价，但未完成第一轮调查
                                                } else {
                                                    res.send('0'); //未完成专家(职业人士)自我评价
                                                }
                                            });
                                        } else {
                                            if (rows[0].s_title != null && rows[0].v_time != null && rows[0].v_time != null)
                                                res.send('1'); //完成专家(学生)自我评价，但未完成第一轮调查
                                            else
                                                res.send('0'); //未完成专家(学生)自我评价
                                        }
                                    });
                                } else {
                                    res.send('2'); //完成第一轮调查，但未完成第二轮调查
                                }
                            });
                        } else {
                            res.send('3'); //完成第二轮调查，但未完成第三轮筛查
                        }
                    });
                } else {
                    res.send('done'); //完成第三轮调查
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    }
});

module.exports = router;